package com.example.syn;

public class WareHouse {
    private int size;
    private int capacity;

    private Goods[] goodsArr;

    public WareHouse(int capacity) {
        this.capacity = capacity;
        goodsArr = new Goods[capacity];
    }

    public void add(Goods goods) {
        synchronized (this) {
            while (size == capacity) {
                System.out.println("warehouse is full, waiting ...");
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            goodsArr[size++] = goods;
            this.notifyAll();
        }
    }

    public Goods get() {
        synchronized (this) {
            while (size == 0) {
                System.out.println("warehouse is empty, waiting ...");
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            Goods g = goodsArr[--size];
            this.notifyAll();

            return g;
        }
    }

}
